<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Login extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('login_model');
    }

    public function index() {
        $this->load->helper('cookie');
        $this->checkCookie();
        $this->form_validation->set_rules('email', 'Email Address', 'valid_email|required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        
        if ($this->form_validation->run() !== false) {
            // then validation passed. Get from db
            $user_result = $this
                    ->login_model
                    ->verify_user(
                    $this->input->post('email'), $this->input->post('password')
            );
            if ($user_result !==FALSE) {
                $this->session->set_userdata('email', $user_result[field('use_email')]);
                $this->session->set_userdata('user_id', $user_result[field('use_id')]);
                if ($this->input->post('remember_me')) {
                    $cookie = array(
                        'name' => 'remember',
                        'value' =>$user_result[field('use_id')],
                        'expire' => 86500,
                        'secure' => FALSE
                    );
                    $this->input->set_cookie($cookie);
                }
                redirect('dashboard');
            }else{
                echo "<script>alert('Invalid Account')</script>";
            }
        }
         $this->load->view('layout/site_master');
        
    }

    public function checkCookie() {
        if ($this->input->cookie("remember")) {
            $iscookie = $this->login_model->checkCookie($this->input->cookie("remember"));
            if ($iscookie && $iscookie[field("pos_title")] == "root") {
                redirect('dashboard');
            }
        }
    }

    public function logout() {
        session_destroy();
        redirect("site");
    }
    public function forgot_form(){
       $this->load->view("forgot_form"); 
    }
    public function reset_password(){
        $random="";
	for($i=0;$i<20;$i++){
	$random.=chr(rand(97,123));
        }
        $this->login_model->set_user_token($this->input->post('email'),$random);
        
        $to = $this->input->post('email');
        //$message = $this->input->post('content');
        $message='<a href="'.base_url()."authentication/login/update_password/".$random.'">click here</a>';
        $this->sendMail($to, $message);
         
    }
 public function sendMail($to, $message) {
        // Load email library
        $this->load->library('email');
        
        // Email configuration 
        // for localhost
        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'mail.vmkhmer.com';
       //  $config['smtp_host'] = 'gsmtp.gmail.com';
        $config['smtp_user'] = 'group_oop@vmkhmer.com';
        $config['smtp_pass'] = '016642011';
        $config['smtp_port'] = 25;
        
        
        $this->email->initialize($config);

        $this->email->from('sale@pichnil.com', 'Pichnil');
        $this->email->to($to);

        $this->email->subject('Reset Password');
        $this->email->message($message);

        $this->email->send();
      //  echo $this->email->print_debugger();
    }
    public function update_password(){
        $this->form_validation->set_rules('password', 'Password', 'required|matches[password_confirm]');
        $this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');
        if ($this->form_validation->run() !== false) {
        $this->login_model->update_password($this->input->post('password'),$this->uri->segment(4));
        }
        $this->load->view("update_password_form");
    }

}

